package com.ktgj.customer.service.impl;

import java.util.Date;
import java.util.List;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.MallCart;
import com.ktgj.customer.domain.vo.MallCartVO;
import com.ktgj.customer.service.IMallCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MallCartProductMapper;
import com.ktgj.customer.domain.MallCartProduct;
import com.ktgj.customer.service.IMallCartProductService;

/**
 * 购物车商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@Service
public class MallCartProductServiceImpl implements IMallCartProductService 
{
    @Autowired
    private MallCartProductMapper mallCartProductMapper;
    @Autowired
    private IMallCartService mallCartService;

    /**
     * 查询购物车商品
     * 
     * @param cartProductId 购物车商品ID
     * @return 购物车商品
     */
    @Override
    public MallCartProduct selectMallCartProductById(Long cartProductId)
    {
        return mallCartProductMapper.selectMallCartProductById(cartProductId);
    }

    /**
     * 查询购物车商品列表
     * 
     * @param mallCartProduct 购物车商品
     * @return 购物车商品
     */
    @Override
    public List<MallCartProduct> selectMallCartProductList(MallCartProduct mallCartProduct)
    {
        return mallCartProductMapper.selectMallCartProductList(mallCartProduct);
    }

    /**
     * 新增购物车商品
     * 
     * @param mallCartProduct 购物车商品
     * @return 结果
     */
    @Override
    public int insertMallCartProduct(MallCartProduct mallCartProduct)
    {
        mallCartProduct.setCreateTime(DateUtils.getNowDate());
        return mallCartProductMapper.insertMallCartProduct(mallCartProduct);
    }

    //添加购物车
    @Override
    public int addMallCartProduct(Long customerId, String openId, MallCartProduct mallCartProduct) {
        List<MallCartVO> mallCartVOS = mallCartService.mallCartList(customerId, openId);
        MallCartVO cartVO = null;
        if (mallCartVOS != null && mallCartVOS.size() > 0) {
            cartVO = mallCartVOS.get(0);
        }
        if (cartVO != null) {
            mallCartProduct.setCartId(cartVO.getCartId());
        } else {
            MallCart cart = new MallCart();
            cart.setCustomerId(customerId);
            if (openId != null && openId != "")
                cart.setOpenId(openId);
            List<MallCart> mallCarts = mallCartService.selectMallCartList(cart);
            if (mallCarts != null && mallCarts.size() > 0)
                BeanUtils.copyProperties(mallCarts.get(0), cart);
            mallCartProduct.setCartId(cart.getCartId());
        }
        MallCartProduct product = new MallCartProduct();
        product.setCartId(mallCartProduct.getCartId());
        product.setProductId(mallCartProduct.getProductId());
        product.setProductSpecId(mallCartProduct.getProductSpecId());
        List<MallCartProduct> mallCartProducts = mallCartProductMapper.selectMallCartProductList(product);
        int i = 0;
        if (mallCartProducts != null && mallCartProducts.size() > 0) {
            if (mallCartProduct.getType() == 1) {
                mallCartProduct.setCartProductId(mallCartProducts.get(0).getCartProductId());
                mallCartProduct.setNumber(mallCartProducts.get(0).getNumber() + mallCartProduct.getNumber());
                mallCartProduct.setUpdateTime(new Date());
                i = mallCartProductMapper.updateMallCartProduct(mallCartProduct);
            } else if (mallCartProduct.getType() == 2) {
                mallCartProduct.setCartProductId(mallCartProducts.get(0).getCartProductId());
                mallCartProduct.setNumber(mallCartProducts.get(0).getNumber() - mallCartProduct.getNumber());
                mallCartProduct.setUpdateTime(new Date());
                i = mallCartProductMapper.updateMallCartProduct(mallCartProduct);
            } else if (mallCartProduct.getType() == 3) {
                i = mallCartProductMapper.deleteMallCartProductById(mallCartProducts.get(0).getCartProductId());
            }
        } else {
            mallCartProduct.setCreateTime(new Date());
            mallCartProduct.setUpdateTime(new Date());
            i = mallCartProductMapper.insertMallCartProduct(mallCartProduct);
        }
        return i;
    }

    /**
     * 修改购物车商品
     * 
     * @param mallCartProduct 购物车商品
     * @return 结果
     */
    @Override
    public int updateMallCartProduct(MallCartProduct mallCartProduct)
    {
        mallCartProduct.setUpdateTime(DateUtils.getNowDate());
        return mallCartProductMapper.updateMallCartProduct(mallCartProduct);
    }

    /**
     * 批量删除购物车商品
     * 
     * @param cartProductIds 需要删除的购物车商品ID
     * @return 结果
     */
    @Override
    public int deleteMallCartProductByIds(Long[] cartProductIds)
    {
        return mallCartProductMapper.deleteMallCartProductByIds(cartProductIds);
    }

    /**
     * 删除购物车商品信息
     * 
     * @param cartProductId 购物车商品ID
     * @return 结果
     */
    @Override
    public int deleteMallCartProductById(Long cartProductId)
    {
        return mallCartProductMapper.deleteMallCartProductById(cartProductId);
    }

    //通过购物车编号与商品编号移除商品
    @Override
    public int deleteMallCartProduct(Long cartId, Long productId) {
        return mallCartProductMapper.deleteMallCartProduct(cartId, productId);
    }
}
